// ProgressBuilder.groovy

package ycoa.ui

public class ProgressBuilder {

  private List itemList = []
  private int selected = 0;

  public ProgressBuilder() {
  }

  public void addItem(String name, String url, int length=10, Boolean selected=false) {
    try {
      def item = [:]
      item['name'] = name.padRight(length, "&nbsp;")
      item['url'] = url;
      item['length'] = length;
      item['selected'] = selected;
      itemList << item
      if(selected) this.selected = itemList.size()
     } catch(e) {
       throw new Exception("Error adding item.", e)
     }
  }

  public String getStyleSheet() {
    return """
      <style type="text/css">
      <!--
      /* progress builder sytle */
      body {
        font-family:"Trebuchet MS, Arial, Helvetica, sans-serif 80%";
        color: #948a63;
        font-weight: bold;
      }
      #div1 p {
        margin-right:0px;
        text-align:left;
        border-top:1px solid #948a63;
        border-bottom:1px solid #948a63;
      }
      .progress {
         float:left;
         background-color: #efefe7;
      }
      .progress-current {
         float:left;
         background-color: #fefe7e;
      }
      .arrow {
         vertical-align: -4px;
      }
      -->
      </style>
    """
  }

  public String getProgressBar() {
    try {
      def sb = new StringBuilder()
      def counter = 0
      itemList.each() { item ->
        def cssClass = "progress"
        def cssStyle = ""
        def imgArrow = "images/progress-arrow-1.gif"
        counter++
        if(selected==counter-1) {
          imgArrow = "images/progress-arrow-left-selected-1.gif"
          cssClass = "progress"
        } else if(selected==counter) {
          imgArrow = "images/progress-arrow-selected-1.gif"
          cssClass = "progress-current"
        } else if(selected==counter+1) {
          imgArrow = "images/progress-arrow-right-selected-1.gif"
          cssClass = "progress"
        } else {
          imgArrow = "images/progress-arrow-1.gif"
          cssClass = "progress"
        }
        if(counter==0) {
          cssStyle = "border-left:1px solid #948a63;"
        } else if(counter==itemList.size()) {
          cssStyle = "border-right:1px solid #948a63;"
        }
        sb << """
                <p class='${cssClass}' style='${cssStyle}'>
                  <img class='arrow' src='spacer.gif' width='19' height='19'/>
                  <a href="${item['url']}">${item['name']}</a>
                  <img class='arrow' src='${imgArrow}' width='19' height='19'/>
                </p>
              """
      }
      return sb.toString()
    } catch(e) {
      throw new Exception("Error rendering progress bar.", e)
    }
  }

}

// *EOF*
